/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package monographsinpdf;

import cz.mzk.rehan.commons.Dom4jUtils;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.dom4j.Document;
import org.dom4j.DocumentException;

/**
 *
 * @author Martin Řehánek <rehan at mzk.cz>
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        //        try {
        //            File monographDtd = new File("/home/rehan/knizky_mlp/data/9788020019219_KOHOUT_Muj_zivot_s_Hitlerem_Stalinem_a_Havlem_I.xml");
        //            Document monograph = Dom4jUtils.loadDocument(monographDtd, false);
        //            File dtdToMods = new File("/home/rehan/knizky_mlp/model_monograph_MODS.xsl");
        //            Document template = Dom4jUtils.loadDocument(dtdToMods, false);
        //            Document mods = Dom4jUtils.transformDocument(monograph, template);
        //            File modsFile = new File("/home/rehan/knizky_mlp/test_mods.xml");
        //            Dom4jUtils.saveDocument(mods, modsFile);
        //        } catch (DocumentException ex) {
        //            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        //        } catch (FileNotFoundException ex) {
        //            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        //        }
                try {
                    PDDocument document = PDDocument.load(new File("/home/rehan/tmp/pdf/test.pdf"));
                    List<PDPage> pages = document.getDocumentCatalog().getAllPages();
        
                    for (int i = 0; i < pages.size(); i++) {
                        PDPage singlePage = pages.get(i);
        
                        BufferedImage buffImage = singlePage.convertToImage(8,12);
                        //convertToImage(singlePage, 8, 12);
                        ImageIO.write(buffImage, "png", new File("/home/rehan/tmp/pdf/page_" + i + ".png"));
                    }
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
//        try {
//            ImageMain.setup();
//        } catch (IOException ex) {
//            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
//        }
    }
//    public static BufferedImage readImage(URL url, ImageMimeType type, int page) throws IOException {
//        LOGGER.fine("type is "+type);
//        if (type == null) return null;
//        if (type.javaNativeSupport()) {
//            return ImageIO.read(url.openStream());
//        } else if ((type.equals(ImageMimeType.DJVU)) || (type.equals(ImageMimeType.VNDDJVU)) || (type.equals(ImageMimeType.XDJVU))) {
//            com.lizardtech.djvu.Document doc = new com.lizardtech.djvu.Document(url);
//            doc.setAsync(false);
//            DjVuPage[] p = new DjVuPage[1];
//            // read page from the document - index 0, priority 1, favorFast true
//            int size = doc.size();
//            if ((page != 0) && (page >= size)) {
//                page = 0;
//            }
//            p[0] = doc.getPage(page, 1, true);
//            p[0].setAsync(false);
//
//            DjVuImage djvuImage = new DjVuImage(p, true);
//            Rectangle pageBounds = djvuImage.getPageBounds(0);
//
//            Image[] images = djvuImage.getImage(new JPanel(), new Rectangle(pageBounds.width, pageBounds.height));
//            if (images.length == 1) {
//                Image img = images[0];
//                if (img instanceof BufferedImage) {
//                    return (BufferedImage) img;
//                } else {
//                    return toBufferedImage(img);
//                }
//            } else
//                return null;
//        } else if (type.equals(ImageMimeType.PDF)) {
//            PDDocument document = null;
//            try {
//                document = PDDocument.load(url.openStream());
//                int resolution = 96;
//                List pages = document.getDocumentCatalog().getAllPages();
//                PDPage pdPage = (PDPage) pages.get(page);
//                BufferedImage image = pdPage.convertToImage(BufferedImage.TYPE_INT_RGB, resolution);
//                return image;
//            } finally {
//                if (document != null) {
//                    document.close();
//                }
//            }
//        } else
//            throw new IllegalArgumentException("unsupported mimetype '" + type.getValue() + "'");
//    }
}
